﻿<html DIR="LTR" xmlns:tool="http://www.microsoft.com/tooltip" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ddue="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:MSHelp="http://msdn.microsoft.com/mshelp">
  <head>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=utf-8" />
    <META NAME="save" CONTENT="history" />
    <title>Modifying Result Set Data Sample</title>
    
    <link rel="stylesheet" type="text/css" href="../local/Classic.css">
      
    </link>
    
    <script src="../local/script.js">
      
    </script><script src="../local/script_main.js">&amp;nbsp;</script>
  </head>
  <body>
    <!--Topic built:09/24/2007 09:01:38-->

    
    
    
    
    
    
    
    
    
    <div id="header">
      <table width="100%" id="topTable"><tr>
          <td align="left">
            <span id="headerBold">Modifying Result Set Data Sample</span>
          </td>
          <td align="right">
            
          </td>
        </tr></table>
      
      
      
    </div>
    <div id="mainSection">
      
        
        
		<font color="DarkGray"><!-- [This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]<br/>[Documentation built on $$TIMESTAMP$$]  --> </font> <p /> <p />
	
        <div id="introductionSection" class="section">
    <p>This Microsoft SQL Server 2005 JDBC Driver sample application demonstrates how to retrieve an updateable set of data from a SQL Server database. Then, using methods of the <a href="eaffcff1-286c-459f-83da-3150778480c9.htm">SQLServerResultSet</a> object, it inserts, modifies, and then finally deletes a row of data from the set of data. </p>
    <p>The code file for this sample is named updateRS.java, and it can be found in the following location:</p>
    <p>&lt;<i>installation directory</i>&gt;\sqljdbc_&lt;<i>version</i>&gt;\&lt;<i>language</i>&gt;\help\samples\resultsets</p>
  </div><h1 class="heading">Requirements</h1><div id="requirementsSection" class="section">
    <content xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5">
      <p xmlns="">To run this sample application, you must set the classpath to include the sqljdbc.jar file. If the classpath is missing an entry for sqljdbc.jar, the sample application will throw the common "Class not found" exception. You will also need access to the SQL Server 2000 AdventureWorks sample database.</p>
      <p xmlns="">For more information about how to set the classpath, see <a href="6faaf05b-8b70-4ed2-9b44-eee5897f1cd0.htm">Using the JDBC Driver</a>.</p>
    </content>
  </div><h1 class="heading">Example</h1><div id="codeExampleSection" class="section">
    <description xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5">
      <content>
        <p xmlns="">In the following example, the sample code makes a connection to the AdventureWorks sample database. Then, using an SQL statement with the <a href="ec24963c-8b51-4838-91e9-1fbfa2347451.htm">SQLServerStatement</a> object, it runs the SQL statement and places the data that it returns into an updateable <b>SQLServerResultSet</b> object.</p>
        <p xmlns="">Next, the sample code uses the <a href="f3c54bfe-d5b7-4f6e-ae6c-3e8954e5b1c9.htm">moveToInsertRow</a> method to move the result set cursor to the insert row, uses a series of <a href="0cf286c7-9dcd-4b02-a568-ba6b6eb27e82.htm">updateString</a> methods to insert data into the new row, and then calls the <a href="363d1008-1396-4fc0-8e27-c9ba2499e7f1.htm">insertRow</a> method to persist the new row of data back to the database.</p>
        <p xmlns="">After inserting the new row of data, the sample code uses an SQL statement to retrieve the previously inserted row, and then uses the combination of <b>updateString</b> and <a href="cfced0ca-a281-40dc-8d2f-370d5f0bf12b.htm">updateRow</a> methods to update the row of data and again persist it back to the database.</p>
        <p xmlns="">Finally, the sample code retrieves the previously updated row of data and then deletes it from the database using the <a href="aa04a644-c7c2-4738-8b6e-7fea566d2c16.htm">deleteRow</a> method.</p>
      </content>
    </description>
    <div class="sampleCode"><span codeLanguage="other"><pre>import java.sql.*;

public class updateRS {

   public static void main(String[] args) {

      // Create a variable for the connection string.
      String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
            "databaseName=AdventureWorks;integratedSecurity=true;";

      // Declare the JDBC objects.
      Connection con = null;
      Statement stmt = null;
      ResultSet rs = null;
 
      try {
         
         // Establish the connection.
         Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
         con = DriverManager.getConnection(connectionUrl);

         // Create and execute an SQL statement, retrieving an updateable result set.
         String SQL = "SELECT * FROM HumanResources.Department;";
         stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
         rs = stmt.executeQuery(SQL);

         // Insert a row of data.
         rs.moveToInsertRow();
         rs.updateString("Name", "Accounting");
         rs.updateString("GroupName", "Executive General and Administration");
         rs.updateString("ModifiedDate", "08/01/2006");
         rs.insertRow();

         // Retrieve the inserted row of data and display it.
         SQL = "SELECT * FROM HumanResources.Department WHERE Name = 'Accounting';";
         rs = stmt.executeQuery(SQL);
         displayRow("ADDED ROW", rs);

         // Update the row of data.
         rs.first();
         rs.updateString("GroupName", "Finance");
         rs.updateRow();

         // Retrieve the updated row of data and display it.
         rs = stmt.executeQuery(SQL);
         displayRow("UPDATED ROW", rs);

         // Delete the row of data.
         rs.first();
         rs.deleteRow();
         System.out.println("ROW DELETED");
      }

      // Handle any errors that may have occurred.
      catch (Exception e) {
         e.printStackTrace();
      }

      finally {
         if (rs != null) try { rs.close(); } catch(Exception e) {}
         if (stmt != null) try { stmt.close(); } catch(Exception e) {}
         if (con != null) try { con.close(); } catch(Exception e) {}
      }
   }

   public static void displayRow(String title, ResultSet rs) {
      try {
         System.out.println(title);
         while (rs.next()) {
            System.out.println(rs.getString("Name") + " : " + rs.getString("GroupName"));
            System.out.println();
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}</pre></span></div>
  </div><span id="seeAlsoSpan"><h1 class="heading">See Also</h1></span><div id="seeAlsoSection" class="section" name="collapseableSection"><a href="4fc4b1c6-3075-4ad7-9244-865d9ede7ae6.htm">Working with Result Sets</a><br /><br /></div><!--[if gte IE 5]>
			<tool:tip element="seeAlsoToolTip" avoidmouse="false"/><tool:tip element="languageFilterToolTip" avoidmouse="false"/><tool:tip element="roleInfoSpan" avoidmouse="false"/>
		<![endif]-->
      <div id="footer" class="section">
        
		<hr />
		
		<span id="fb" class="feedbackcss">
			
			
		</span>
		
		<a href="9bad553b-9e70-4696-8499-2e35f772a1e0.htm">
			
			© 2007 Microsoft Corporation. All rights reserved.
		</a>
 	
	
      </div>
    </div>
  </body>
</html>